﻿; 慢步 DotA IMBA 专用热键
; Made by 慢步者
; 2013-08

; 在 Windows 7 / Vista 操作系统下运行，需要管理员权限，否则热键不起作用
If Not A_IsAdmin
{
	Run *RunAs "%A_ScriptFullPath%"
	ExitApp
}

MsgBox, 0, 提示, 慢步 DotA IMBA 专用热键 V1.2`n`n◆ `` 键 = 数字键盘 7`n◆ Tab 键 = 数字键盘 4`n◆ Caps Lock 键 = 数字键盘 1`n◆ Alt + `` 键 = 数字键盘 8`n◆ Alt + Tab 键 = 数字键盘 5`n◆ Alt + Caps Lock 键 = 数字键盘 2`n◆ Q 键 = 数字键盘 8 + Q`n◆ Z 键 = 数字键盘 5 + Z`n◆ Win 键 = 数字键盘 2`n◆ F2 键快速输入 -hfbb`n◆ F3 键快速输入 -hfrd`n◆ F4 键快速输入 -ma`n◆ F5 键快速给近卫商店编号`n◆ F6 键快速给天灾商店编号`n◆ F7 键快速调用零度魔兽助手设置视野`n◆ F8 键快速设置共享信使`n◆ F9 键快速输入 -swap `n◆ Ctrl + 鼠标中键记录鼠标位置，之后在屏幕任意位置单独按 N 键时自动用鼠标左键点击已记录的鼠标位置（可解决两项技能快捷键冲突问题），按鼠标中键或按空格键更能恢复当前鼠标位置及按鼠标左键`n`n◆ Pause 键启用 / 禁用热键

; 限制程序以单实例运行
#SingleInstance force

; disable the warning dialog if a key is held down
#HotkeyInterval 0

; Forces the unconditional installation of the keyboard hook
#InstallKeybdHook

; might increase responsiveness of hotkeys
#UseHook On

; use 20 (the max) instead of 10 threads
#MaxThreads 20

; makes the script run at max speed
SetBatchLines, -1

; faster response (might be better with -1, 0)
SetKeyDelay, -1, -1

; 设定托盘图标形状为 Warcraft III 的可执行文件图标
RegRead, war, HKEY_CURRENT_USER, Software\Blizzard Entertainment\Warcraft III, ProgramX
Menu, tray, icon, %war%, 2, 1

; 启用/禁用热键的变量
Enabled := True

; 定义快速给商店编号用到的全局变量
SampleScreenWidth := 1440
SampleScreenHeight := 900
PositionArray := Object()

; 读取商店位置数据
ReadJingWeiData()
ReadTianZaiData()

; 热键开关，按下 Pause 键切换启用/禁用热键
*Pause::
	Suspend, Toggle
	if (Enabled == False)
	{
		;Suspend, On
		Enabled := True
		SoundPlay, *48
	}
	else
	{
		;Suspend, Off
		Enabled := False
		;SoundPlay, *64
		SoundPlay, *16
	}
	return

; 以下内容仅在 Warcraft III 窗口激活的情况下才被执行
#ifWinActive ahk_class Warcraft III

; 设定按 ` / Tab / Caps Lock 键与小键盘的数字键 7 / 4 / 1 效果相同
`::Send, {Numpad7}
Tab::Send, {Numpad4}{Tab}
CapsLock::Send, {Numpad1}{CapsLock}
return

; 设定按 Alt + ` / Alt + Tab / Alt + Caps Lock / Win 键与小键盘的数字键 8 / 5 / 2 / 2 效果相同
!`::Send, {Numpad8}
!Tab::Send, {Numpad5}
!CapsLock::Send, {Numpad2}
Lwin::Send, {Numpad2}
return

; 设定按 q / z 键时多按一下小键盘的数字键 8 / 5，便于使用物品
q::Send, {Numpad8}q
z::Send, {Numpad5}z
return

; 记录按下 Ctrl + 鼠标中键时的鼠标位置的变量
MClickPosX := 0
MClickPosY := 0

; Ctrl + 鼠标中键按下时记录鼠标位置
^Mbutton::
	MouseGetPos, MClickPosX, MClickPosY
	SoundPlay, *64
	return

; 鼠标中键单独按下或空格键按下时模拟左键点击已记录的鼠标位置（当随机赠送的技能快捷键与英雄自有技能的快捷键冲突时，以此代替快捷键失效的技能），并恢复鼠标位置
*Mbutton::
Space::
	MouseGetPos, CurrentX, CurrentY
	MouseClick, Left, MClickPosX, MClickPosY
	Sleep, 100
	MouseClick, Left, CurrentX, CurrentY
	return

; 按 n 键时模拟左键点击已记录的鼠标位置（当随机赠送的技能快捷键与英雄自有技能的快捷键冲突时，以此代替快捷键失效的技能）
n::
	MouseClick, Left, MClickPosX, MClickPosY
	Send, n
	return

; 屏蔽 Win 键
;Lwin::
;	return

; 按 F2 键快速输入 -hfbb
F2::
	Suspend permit
	Send {Enter}
	Sleep 10
	Send -hfbb
	return

; 按 F3 键快速输入 -hfrd
F3::
	Suspend permit
	Send {Enter}
	Sleep 10
	Send -hfrd
	return

; 按 F4 键快速输入 -ma
F4::
	Suspend permit
	Send {Enter}
	Sleep 10
	Send -ma
	Send {Enter}
	return

; 按 F5 键快速给近卫商店编号（以 1440 x 900 的屏幕分辨率为基准，通过换算适用其他屏幕分辨率）
F5::
	WinGetPos, , , ScreenWidth, ScreenHeight, Warcraft III

	MouseClick, Left, Round(PositionArray[1, 0, 1] * ScreenWidth / SampleScreenWidth), Round(PositionArray[1, 0, 2] * ScreenHeight / SampleScreenHeight)
	Sleep, 200

	Send, {Ctrl Down}
	LoopCount := PositionArray[1, 0, 0]
	Loop %LoopCount%
	{
		If PositionArray[1, A_Index, 1] >= 0 && PositionArray[1, A_Index, 2] >= 0
		{
			MouseClick, Left, Round(PositionArray[1, A_Index, 1] * ScreenWidth / SampleScreenWidth), Round(PositionArray[1, A_Index, 2] * ScreenHeight / SampleScreenHeight)
			Sleep, 100
			Key := Mod(A_Index, 10)
			Send %Key%
		}
	}
	Send, {Ctrl Up}
	return

; 按 F6 键快速给天灾商店编号（以 1440 x 900 的屏幕分辨率为基准，通过换算适用其他屏幕分辨率）
F6::
	WinGetPos, , , ScreenWidth, ScreenHeight, Warcraft III

	MouseClick, Left, Round(PositionArray[2, 0, 1] * ScreenWidth / SampleScreenWidth), Round(PositionArray[2, 0, 2] * ScreenHeight / SampleScreenHeight)
	Sleep, 200

	Send, {Ctrl Down}
	LoopCount := PositionArray[2, 0, 0]
	Loop %LoopCount%
	{
		If PositionArray[2, A_Index, 1] >= 0 && PositionArray[2, A_Index, 2] >= 0
		{
			MouseClick, Left, Round(PositionArray[2, A_Index, 1] * ScreenWidth / SampleScreenWidth), Round(PositionArray[2, A_Index, 2] * ScreenHeight / SampleScreenHeight)
			Sleep, 100
			Key := Mod(A_Index, 10)
			Send %Key%
		}
	}
	Send, {Ctrl Up}
	return

; 按 F7 键快速调用零度魔兽助手设置视野
F7::
	Send, ^p
	Sleep, 100
	Send, !{PgUp}
	Sleep, 100
	Send, !{PgUp}
	Sleep, 100
	Send, !{PgUp}
	Sleep, 100
	Send, !{PgUp}
	return

; 按 F8 键快速设置共享信使
F8::
	Send, {F11}
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(225 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(275 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(325 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(375 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(420 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(470 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(520 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(570 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(1150 * ScreenWidth / 1920), Round(620 * ScreenHeight / 1080)
	Sleep, 100
	MouseClick, Left, Round(795 * ScreenWidth / 1920), Round(855 * ScreenHeight / 1080)
	return

; 按 F9 键快速输入 -swap
F9::
	Suspend permit
	Send {Enter}
	Sleep 10
	Send -swap{Space}
	return

; 按 F10 键快速输入 -om / -onlymid（中路模式，上下两路既不出兵，塔也无敌状态。等于 -ntnb）
F10::
	Suspend permit
	Send {Enter}
	Sleep 10
	Send -om
	return

; 读取近卫商店位置数据
JingWeiLineIndex := 1

ReadJingWeiData()
{
	Global SampleScreenWidth
	Global SampleScreenHeight
	Global PositionArray
	Global JingWeiLineIndex

	JingWeiLineIndex := 1

	SampleScreenWidth := ReadJingWeiLine()
	SampleScreenHeight := ReadJingWeiLine()

	ClickX := ReadJingWeiLine()
	ClickY := ReadJingWeiLine()

	PositionArray[1, 0, 1] := ClickX
	PositionArray[1, 0, 2] := ClickY

	Loop
	{
		Line := ReadJingWeiLine()
		If StrLen(Line) = 0
			break
		PositionArray[1, A_Index, 1] := Line

		Line := ReadJingWeiLine()
		If StrLen(Line) = 0
			break
		PositionArray[1, A_Index, 2] := Line

		PositionArray[1, 0, 0] := A_Index
	}
}

ReadJingWeiLine()
{
	Global JingWeiLineIndex

	Loop
	{
		FileReadLine, Line, JingWeiShopsPosition.txt, JingWeiLineIndex

		JingWeiLineIndex++

		If ErrorLevel
			break

		CommentMark := ";"
		StringGetPos Pos, Line, %CommentMark%

		If Pos >= 0
			Line := SubStr(Line, 1, Pos)

		Line := Trim(Line)

		If StrLen(Line) > 0
			break
	}

	return Line
}

; 读取天灾商店位置数据
TianZaiLineIndex := 1

ReadTianZaiData()
{
	Global SampleScreenWidth
	Global SampleScreenHeight
	Global PositionArray
	Global TianZaiLineIndex

	TianZaiLineIndex := 1

	SampleScreenWidth := ReadTianZaiLine()
	SampleScreenHeight := ReadTianZaiLine()

	ClickX := ReadTianZaiLine()
	ClickY := ReadTianZaiLine()

	PositionArray[2, 0, 1] := ClickX
	PositionArray[2, 0, 2] := ClickY

	Loop
	{
		Line := ReadTianZaiLine()
		If StrLen(Line) = 0
			break
		PositionArray[2, A_Index, 1] := Line

		Line := ReadTianZaiLine()
		If StrLen(Line) = 0
			break
		PositionArray[2, A_Index, 2] := Line

		PositionArray[2, 0, 0] := A_Index
	}
}

ReadTianZaiLine()
{
	Global TianZaiLineIndex

	Loop
	{
		FileReadLine, Line, TianZaiShopsPosition.txt, TianZaiLineIndex

		TianZaiLineIndex++

		If ErrorLevel
			break

		CommentMark := ";"
		StringGetPos Pos, Line, %CommentMark%

		If Pos >= 0
			Line := SubStr(Line, 1, Pos)

		Line := Trim(Line)

		If StrLen(Line) > 0
			break
	}

	return Line
}
